package com.nononsenseapps.feeder.db.room

import androidx.room.ColumnInfo
import androidx.room.Ignore
import com.nononsenseapps.feeder.db.FEED_ITEM
import com.nononsenseapps.feeder.db.FEED_ITEM_FEED
import com.nononsenseapps.feeder.util.sloppyLinkToStrictURLNoThrows
import org.joda.time.DateTime
import java.net.URI
import java.net.URL

/*const val feedItemColumnsWithFeed = "$FEED_ITEMS_TABLE_NAME.$COL_ID AS $COL_ID," +
        " $COL_GUID, "+" +
        "$FEED_ITEMS_TABLE_NAME.$COL_TITLE AS $COL_TITLE, " +
        "$COL_DESCRIPTION,
$COL_PLAINTITLE,
$COL_PLAINSNIPPET,
$FEED_ITEMS_TABLE_NAME.$COL_IMAGEURL,
$COL_ENCLOSURELINK,
$FEED_ITEMS_TABLE_NAME.$COL_CREATEDATE AS $COL_CREATEDATE,
$FEED_ITEMS_TABLE_NAME.$COL_ITEM_TAGS AS $COL_ITEM_TAGS, " +
        "$COL_AUTHOR,
$COL_PUBDATE,
$COL_LINK,
$COL_UNREAD,
$FEEDS_TABLE_NAME.$COL_TAG AS $COL_TAG,
$FEEDS_TABLE_NAME.$COL_ID AS $COL_FEEDID,
$FEED_ITEMS_TABLE_NAME.$COL_IMAGE_COUNT AS $COL_IMAGE_COUNT, " +
        "$FEEDS_TABLE_NAME.$COL_TITLE AS $COL_FEEDTITLE, " +
        "$FEEDS_TABLE_NAME.$COL_CUSTOM_TITLE AS $COL_FEEDCUSTOMTITLE, " +
        "$FEEDS_TABLE_NAME.$COL_URL AS $COL_FEEDURL"*/


const val feedItemColumnsWithFeed="""feed_items.id as id,
      feed_items.guid as guid, 
      feed_items.title as title, 
      feed_items.feed_id as feed_id,
      feed_items.description as description, 
      feed_items.plain_title as plain_title, 
      feed_items.plain_snippet as plain_snippet,
      feed_items.image_url as image_url, 
      feed_items.item_feature as item_feature, 
      feed_items.enclosure_link as enclosure_link, 
      feed_items.create_date as create_date, 
      feed_items.item_tags as item_tags, 
      feed_items.author as author, 
      feed_items.pub_date as pub_date, 
      feed_items.link as link, 
      feed_items.unread as unread, 
      feed_items.image_count as image_count, 
      feed_items.word_count as word_count, 
      feeds.tag as tag, 
      feeds.title as feed_title,
      feeds.custom_title as feed_customtitle,
      feeds.url as feed_url
"""

data class FeedItemWithFeed @Ignore constructor(
    var id: Long = ID_UNSET,
    var guid: String = "",
    var title: String = "",
    var description: String = "",
    @ColumnInfo(name = FEED_ITEM.COL_PLAIN_TITLE) var plainTitle: String = "",
    @ColumnInfo(name = FEED_ITEM.COL_PLAIN_SNIPPET) var plainSnippet: String = "",
    @ColumnInfo(name = FEED_ITEM.COL_IMAGE_URL) var imageUrl: String? = null,
    @ColumnInfo(name = FEED_ITEM.COL_ENCLOSURE_LINK) var enclosureLink: String? = null,
    @ColumnInfo(name = FEED_ITEM.COL_ITEM_FEATURE)  var feature: Long? = null,
    var author: String? = null,
    @ColumnInfo(name = FEED_ITEM.COL_PUB_DATE) var pubDate: DateTime? = null,
    var link: String? = null,
    @ColumnInfo(name = FEED_ITEM.COL_WORD_COUNT) var wordCount: Int = 0,
    var tag: String = "",
    var unread: Boolean = true,
    @ColumnInfo(name = FEED_ITEM.COL_IMAGE_COUNT) var image_count: Int = 0,
    @ColumnInfo(name = FEED_ITEM.COL_FEED_ID) var feedId: Long? = null,
    @ColumnInfo(name = FEED_ITEM_FEED.COL_FEEDTITLE) var feedTitle: String = "",
    @ColumnInfo(name = FEED_ITEM_FEED.COL_FEEDCUSTOMTITLE) var feedCustomTitle: String = "",
    @ColumnInfo(name = FEED_ITEM_FEED.COL_FEEDURL) var feedUrl: URL = sloppyLinkToStrictURLNoThrows(""),
    @ColumnInfo(name = FEED_ITEM.COL_CREATE_DATE) var createDate: DateTime? = null,
    @ColumnInfo(name = FEED_ITEM.COL_ITEM_TAGS) var itemTags: String = ""
) {
    constructor() : this(id = ID_UNSET)

    val feedDisplayTitle: String
         get() = if (feedCustomTitle.isBlank()) feedTitle else feedCustomTitle

    val enclosureFilename: String?
         get() {
            if (enclosureLink != null) {
                var fname: String? = null
                try {
                    fname = URI(enclosureLink).path.split("/").last()
                } catch (e: Exception) {
                }
                if (fname == null || fname.isEmpty()) {
                    return null
                } else {
                    return fname
                }
            }
            return null
        }

    val pubDateString: String?
        get() = pubDate?.toString()

    val domain: String?
         get() {
            val l: String? = enclosureLink ?: link
            if (l != null) {
                try {
                    return URL(l).host.replace("www.", "")
                } catch (e: Throwable) {
                }
            }
            return null
        }

}
